package com.example.mq;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import java.io.UnsupportedEncodingException;

@SpringBootApplication
@EnableConfigurationProperties
public class MqApp {

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, InterruptedException, RemotingException {

        SpringApplication.run(MqApp.class, args);


//        // 实例化消息生产者Producer
//        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
//        // 设置NameServer的地址
//        producer.setNamesrvAddr("localhost:9876");
//        // 启动Producer实例
//        producer.start();
//        User msg = new User("hello" /* Topic */,
//                "TagA" /* Tag */,
//                ("Hello RocketMQ 3" ).getBytes(RemotingHelper.DEFAULT_CHARSET) /* User body */
//        );
//        // 设置延时时间, 10秒钟后消费者才会去消费消息
//        msg.setDelayTimeLevel(3);
//        // 同步发送，等待Broker响应
////        SendResult send = producer.send(msg);
////        System.out.println(send);
//        // 异步发送
//        producer.send(msg, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                System.out.println(sendResult);
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                System.out.println("没有发送成功");
//            }
//        });
//        System.out.println("开始异步发送消息");
//        Thread.sleep(5000);
//        // 如果不再发送消息，关闭Producer实例。
////        producer.shutdown();
//        System.out.println("结束了吗");
//    }
    }
}
